<!DOCTYPE html>
<html debug="true">
  <head>
    <script type="text/javascript" src="../../../../../ext/adapter/ext/ext-base.js"></script>
    <script type="text/javascript" src="../../../../../ext/ext-all-debug.js"></script>

    <script type="text/javascript" src="../../../../../openlayers/lib/OpenLayers.js"></script>
    <script type="text/javascript" src="../../../../lib/GeoExt.js"></script>
    <script type="text/javascript" src="WMSCapabilitiesReader.js"></script>

    <script type="text/javascript">
      
        function test_constructor(t) {
            t.plan(2);
            var reader = new GeoExt.data.WMSCapabilitiesReader();

            var fields = reader.recordType.prototype.fields;

            // 1 test
            t.eq(fields.items.length, 24, 'number of default items is correct');

            var reader = new GeoExt.data.WMSCapabilitiesReader({},[
                {name: "foo"},
                {name: "bar"}
            ]);

            var fields = reader.recordType.prototype.fields;

            //1 test
            t.ok(fields.items[2].name == 'foo' &&
                 fields.items[3].name == 'bar',
                 'field values set from configuration are correct');
        }
        function test_read(t) {
            t.plan(39);

            // test a reader with the only two default LayerRecord fields
            var reader = new GeoExt.data.WMSCapabilitiesReader({}, []);

            var records = reader.read({responseXML: doc});

            //1 test
            t.eq(records.records[0].fields.items.length, 2, 'LayerRecord with 2 default fields');

            var record = records.records[2];
            //2 tests -- testing the fields of a record
            t.eq(record.get("title"), "tiger:tiger_roads", "correct layer title");
            t.eq(record.get("abstract"), undefined, "correct undefined abstract which is not part of fields");

            // test a reader with all default fields

            var reader = new GeoExt.data.WMSCapabilitiesReader({
                layerOptions: {
                    singleTile: true
                }
            });
            var records = reader.read({responseXML: doc});

            //1 test
            t.eq(records.totalRecords, 22, 'readRecords returns correct number of records');

            var record, layer;

            //1 test -- testing value of record id
            record = records.records[2];
            t.ok(record.id == record.get("layer").id, "[2] correct record id");

            //22 tests -- testing the fields of a record
            record = records.records[2];
            t.eq(record.get("name"), "tiger:tiger_roads", "[2] correct layer name");
            t.eq(record.get("title"), "Manhattan (NY) roads", "[2] correct layer title");
            t.eq(
                record.get("abstract"),
               "Highly simplified road layout of Manhattan in New York..",
                "[2] correct layer abstract"
            );
            t.eq(
                record.get("llbbox"),
                [-74.08769307536667, 40.660618924633326, -73.84653192463333, 40.90178007536667],
                "[2] correct layer bbox"
            );
            t.eq(record.get("styles").length, 1, "[2] correct styles length");
            t.eq(record.get("styles")[0].name, "tiger_roads", "[2] correct style name");
            t.eq(
                record.get("styles")[0].legend.href,
                "http://publicus.opengeo.org:80/geoserver/wms/GetLegendGraphic?VERSION=1.0.0&FORMAT=image/png&WIDTH=20&HEIGHT=20&LAYER=tiger:tiger_roads",
                "[2] correct legend url"
            );
            t.eq(record.get("queryable"), true, "[2] correct queryable attribute");
            t.eq(record.get("opaque"), true, "[2] correct opaque attribute");
            t.eq(record.get("cascaded"), 3, "[2] correct cascaded attribute");
            t.eq(record.get("noSubsets"), true, "[2] correct noSubsets attribute");
            t.eq(record.get("fixedWidth"), 400, "[2] correct fixedWidth attribute");
            t.eq(record.get("fixedHeight"), 200, "[2] correct fixedHeight attribute");
            t.eq(record.get("formats")[0], "image/png", "[2] correct image/png formats attribute");
            t.eq(record.get("formats")[14], "image/geotiff", "[2] correct image/geotiff formats attribute");
            t.eq(record.get("formats")[27], "rss", "[2] correct rss formats attribute");            
            var srs = record.get("srs");
            var count = 0;
            for (var key in srs) {
                if (srs.hasOwnProperty(key)) {
                    count++;
                }
            }
            t.eq(count, 3912, "[2] correct number of supported srs");
            t.eq(srs["EPSG:42303"], true, "[2] random srs in list is supported");
            t.eq(record.get("prefix"), "tiger", "[2] correct prefix attribute");
            t.eq(
                record.get("bbox")["EPSG:4326"].bbox,
                [-74.02722,40.684221,-73.907005,40.878178],
                "[2] correct bbox"
            );
            t.eq(record.get("authorityURLs").gcmd, "http://www.authority.com", "[2] correct authorityURLs attribute");
            t.eq(record.get("identifiers").gcmd, "id_value", "[2] correct identifiers attribute");
            // cannot test "dimensions" until we have nested layers: see http://trac.openlayers.org/ticket/2144

            //7 tests -- Testing the layer field
            record = records.records[2];
            layer = record.get("layer");
            t.eq(layer.CLASS_NAME, "OpenLayers.Layer.WMS", "[2] layer field is of type OpenLayers.Layer.WMS");
            t.eq(layer.url, "http://publicus.opengeo.org:80/geoserver/wms?SERVICE=WMS&", "[2] layer field has correct URL");
            t.eq(layer.params.LAYERS, "tiger:tiger_roads","[2] layer field has correct LAYERS parameter");
            t.eq(layer.name, "Manhattan (NY) roads","[2] layer field has correct name");
            t.eq(typeof(layer.minScale), "number","[2] layer minScale has been set");
            t.eq(typeof(layer.maxScale), "number","[2] layer maxScale has been set");
            t.eq(layer.singleTile, true, "[2] layer field has correct singleTile attribute (from WMSCapabilitiesReader constructor)");
            
            // 3 tests -- attribution markup
            record = records.records[2];
            layer = record.get("layer");
            var attribution = layer.attribution;
            t.ok(attribution.indexOf("http://foo/logo.png") !== -1, "attribution markup has a logo");
            t.ok(attribution.indexOf("Foo Authority") !== -1, "attribution markup has a title");
            t.ok(attribution.indexOf("http://foo/about/") !== -1, "attribution has a link");
            
            // 2 tests - transparent param
            var transparent;
            record = records.records[2];
            layer = record.get("layer");
            transparent = layer.params.TRANSPARENT;
            t.eq(transparent, false,
                "TRANSPARENT param is false if opacity set to 1");
            record = records.records[1];
            layer = record.get("layer");
            transparent = layer.params.TRANSPARENT;
            t.eq(transparent, true,
                "TRANSPARENT param is true if no opacity set");
        }
    </script>
  <body>
    <div id="map"></div>
  </body>
</html>
